package com.af.soldiers.timer;

import com.af.soldiers.config.Monitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;


@Component
public class Heart {

    @Autowired
    private Monitor monitor;

    private static final Logger logger = LoggerFactory.getLogger(Heart.class);

    HttpComponentsClientHttpRequestFactory httpRequestFactory;

    public Heart(){
        httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(1000 * 10);
        httpRequestFactory.setConnectTimeout(1000 * 10);
        httpRequestFactory.setReadTimeout(1000 * 10);
    }

    @Scheduled(cron = "0 * * * * ? ")
    public void selfHeart(){
        try {
            logger.info("begin to Heart");
            ResponseEntity<String> forEntity = new RestTemplate(httpRequestFactory).getForEntity((monitor.getBackUrl() + "heart/jump/" + monitor.getId()), String.class);
            logger.info("heart result is==>" + forEntity.getBody());
        }catch (Exception e){
            logger.error("心跳异常:" , e);
        }

    }
}
